/*
 * Copyright 2013 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.trigger;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Scope;

import com.fujitsu.nanjing.tieba.common.ThirdVelocityEmailService;
import com.fujitsu.nanjing.tieba.constrant.CommonParamter;
import com.fujitsu.nanjing.tieba.ibator.CoreTopic;
import com.fujitsu.nanjing.tieba.ibator.CoreUser;
import com.fujitsu.nanjing.tieba.service.TopicService;
import com.fujitsu.nanjing.tieba.service.UserService;
import com.fujitsu.nanjing.tieba.view.velocity.tools.DomainTool;

/**
 * DailyTrigger.java
 * 
 * @author Administrator
 */
@Configurable("dailyTrigger")
@Scope
@DependsOn
public class DailyTrigger implements Job {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(DailyTrigger.class);

	private TopicService topicService;
	private UserService userService;
	private ThirdVelocityEmailService thirdVelocityEmailService;
	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		perform();
		logger.info("DailyTrigger执行！" + new Date().toString());
	}
	public void perform() {
		notifyLongTimeNoSee();
		Date now = new Date();
		for (int i = 0;; i++) {
			List<CoreTopic> topics = topicService
					.findTopicsHighLightOrTopScope(i * 10);
			if (topics == null || topics.size() == 0) {

				return;
			}
			for (CoreTopic topic : topics) {
				Date high = topic.getHighexpiredate();
				Date top = topic.getTopexpiredate();
				if (high != null) {
					if (now.after(high)) {
						// 回复颜色
						topic.setHighcolor("black");
					}
				}
				if (top != null) {
					if (now.after(top)) {
						topic.setTopscope("N");
					}
				}
				topicService.update(topic);
			}
		}

	}
	private void notifyLongTimeNoSee() {

		for (int i = 0;; i++) {
			Date notifTime = new Date();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(notifTime);
			calendar.add(
					Calendar.DATE,
					-1
							* Integer.parseInt(new DomainTool()
									.info("longTimeNoSee")));
			notifTime = calendar.getTime();
			List<CoreUser> users = userService.findLongTimeNoSee(
					CommonParamter.默认分页长度 * i, notifTime);
			if (users == null || users.size() == 0) {
				return;
			}
			for (CoreUser user : users) {

				Map<String, Object> map = new HashMap<String, Object>();
				map.put("user", user);
				String subject = "亲爱的" + user.getNickname() + ",您已经超过7天没有来过"
						+ new DomainTool().getDomain();
				thirdVelocityEmailService.sendEmail(map, subject,
						"com/fujitsu/mailTemplates/longTime.vm",
						new String[]{user.getEmail()}, new String[]{});
			}
		}

	}
	public TopicService getTopicService() {
		return topicService;
	}
	public void setTopicService(TopicService topicService) {
		this.topicService = topicService;
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public ThirdVelocityEmailService getThirdVelocityEmailService() {
		return thirdVelocityEmailService;
	}
	public void setThirdVelocityEmailService(
			ThirdVelocityEmailService thirdVelocityEmailService) {
		this.thirdVelocityEmailService = thirdVelocityEmailService;
	}

}
